//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.IdentityModel.Tokens;
using System.ServiceModel;
using System.IdentityModel.Claims;

namespace Shp.Security.BrokeredReceiver
{
    /// <summary>
    /// The authentication security token service.
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class AuthenticationSecurityTokenService : SecurityTokenService
    {
        /// <summary>
        /// Sets up the AuthenticationSecurityTokenService by loading relevant Application Settings
        /// </summary>
        public AuthenticationSecurityTokenService()
            : base(ServiceConfiguration.SecurityTokenServiceName,
                   FederationUtilities.GetX509TokenFromCert(
                        ServiceConfiguration.StsStoreName, 
                        ServiceConfiguration.StsStoreLocation, 
                        ServiceConfiguration.StsDistinguishedName),
                   FederationUtilities.GetScopedTokens(
                        ServiceConfiguration.ScopedCertificates))
        {
        }

        /// <summary>
        /// Method for setting up claims in the SAML Token issued by the STS
        /// Should be overriden by STS implementations deriving from this base class
        /// to set up appropriate claims
        /// </summary>
        /// <param name="requestSecurityToken"></param>
        /// <returns></returns>
        protected override Collection<SamlAttribute> GetIssuedClaims(RequestSecurityToken requestSecurityToken)
        {
            // If we get here, we should be already authenticated by the settings 
            // specified in the WCF configuration section.
            // so go ahead and add the auth token            

            string caller = ServiceSecurityContext.Current.PrimaryIdentity.Name;
            Collection<SamlAttribute> samlAttributes = new Collection<SamlAttribute>();
            samlAttributes.Add(new SamlAttribute(new Claim(ClaimTypes.Authentication, caller, Rights.PossessProperty)));

            return samlAttributes;
        }
    }
}
